/*
 *  Copyright (C) 2010 fabien
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.service.impl;

import com.boc.botv.common.Database;
import com.boc.botv.dao.CategoryDao;
import com.boc.botv.model.Category;
import com.boc.botv.service.CategoryManager;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author fabien
 */
@Service("categoryManager")
//@Transactional(readOnly = true)
public class CategoryManagerImpl implements CategoryManager {

  private final Log logger = LogFactory.getLog(SiteAccountManagerImpl.class);
  private CategoryDao categoryDao;

  @Autowired
  public void setCategoryDao(CategoryDao categoryDao) {
    this.categoryDao = categoryDao;
  }

  public List<Category> getChilds(int parentId) {
    logger.info("[CategoryManager] Getting childs of #" + parentId);
         List<Category> cl = categoryDao.getChildsOf(parentId);
    
    return cl;
  }

  public Category getCategory(Integer id) {
    logger.info("[CategoryManager] Looking for category #" + id);
         Category c = categoryDao.getCategory(id);
    List<Category> sub = categoryDao.getChildsOf(c.getId());
    if (sub != null) {
      c.setCountOfChilds(sub.size());
    }
    
    return c;
  }

  public Category getCategory(String name) {
    logger.info("[CategoryManager] Looking for category `" + name + "`");
         Category c = categoryDao.getCategory(name);
    List<Category> sub = categoryDao.getChildsOf(c.getId());
    if (sub != null) {
      c.setCountOfChilds(sub.size());
    }
    
    return c;
  }

  public List<Category> getCategory(){
    logger.info("[CategoryManager] getting all the Category");
         List<Category> cl = categoryDao.getCategory();
    
    return cl;
  }

  //@Transactional(readOnly = false)
  public boolean addCategory(String name, Integer parentId) {
    logger.info("[CategoryManager] Adding category `" + name + "` as a child of #" + parentId);
    if (parentId == null || parentId < 0 || name == null || name.length() < 2) {
      return false;
    }
         Category c = new Category(name, parentId);
    categoryDao.saveCategory(c);
    
    return true;
  }
}
